﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WCFMINERP.Contracts.DataContracts;
using WCFMINERP.DAL;
using WCFMINERP.COM;

namespace WCFMINERP.BLL.Query
{
    public class InfoQueryBLL
    {
        /// <summary>
        /// 根据查询组织产品信息
        /// </summary>
        /// <param name="inArray"></param>
        /// <returns></returns>
        public List<OrderInfoQuary> OrderQuery(string[] inArray,int shopID)
        {
            List<OrderInfoQuary> rtnList = new List<OrderInfoQuary>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                string ctmName = inArray[0];
                string productName = inArray[1];
                DateTime startTime = Convert.ToDateTime(inArray[2]);
                DateTime endTime = Convert.ToDateTime(inArray[3]);
                int comInt = (int)OrderInfoStatus.Completed;
                if (string.IsNullOrEmpty(ctmName) && string.IsNullOrEmpty(productName))//时间选择
                {
                    List<orderInfo> listOrder = db.orderInfo.Where(o => o.status != comInt && o.Tb_OrderBag.Shop.ShopId == shopID && o.Tb_OrderBag.OrderTime > startTime && o.Tb_OrderBag.OrderTime < endTime).ToList();
                    rtnList = OrderConvertToList(db, listOrder);
                }
                else if (!string.IsNullOrEmpty(ctmName) && string.IsNullOrEmpty(productName))
                {
                    List<orderInfo> listOrder = db.orderInfo.Where(o => o.status != comInt && o.Tb_OrderBag.Shop.ShopId == shopID && o.Tb_OrderBag.OrderTime > startTime && o.Tb_OrderBag.OrderTime < endTime && o.CtmName.Contains(ctmName)).ToList();
                    rtnList = OrderConvertToList(db, listOrder);
                }
                else if (!string.IsNullOrEmpty(ctmName) && !string.IsNullOrEmpty(productName))
                {
                    List<orderInfo> listOrder = db.orderInfo.Where(o => o.status != comInt && o.Tb_OrderBag.Shop.ShopId == shopID && o.Tb_OrderBag.OrderTime > startTime && o.Tb_OrderBag.OrderTime < endTime && o.CtmName.Contains(ctmName) && o.OrderProductName.Contains(productName)).ToList();
                    rtnList = OrderConvertToList(db, listOrder);
                }
                else
                {
                    rtnList = new List<OrderInfoQuary>();
                }
                if (!string.IsNullOrEmpty(ctmName))
                {
                    List<orderInfo> listOrder = db.orderInfo.Where(o => o.Tb_OrderBag.Shop.ShopId == shopID && o.Tb_OrderBag.OrderTime > startTime && o.Tb_OrderBag.OrderTime < endTime && (o.CtmName.Contains(ctmName) || o.OrderProductName.Contains(ctmName))).ToList();
                    rtnList.AddRange(OrderConvertToList(db, listOrder));
                    List<orderInfo_BK> listOrderBK = db.orderInfo_BK.Where(o => o.Tb_OrderBag_BK.Shop.ShopId == shopID && o.Tb_OrderBag_BK.OrderTime > startTime && o.Tb_OrderBag_BK.OrderTime < endTime && (o.CtmName.Contains(ctmName) || o.OrderProductName.Contains(ctmName))).ToList();
                    rtnList.AddRange(OrderBKConvertToList(db, listOrderBK));
                }
                else
                {
                    List<orderInfo> listOrder = db.orderInfo.Where(o => o.Tb_OrderBag.Shop.ShopId == shopID && o.Tb_OrderBag.OrderTime > startTime && o.Tb_OrderBag.OrderTime < endTime).ToList();
                    rtnList.AddRange(OrderConvertToList(db, listOrder));
                    List<orderInfo_BK> listOrderBK = db.orderInfo_BK.Where(o => o.Tb_OrderBag_BK.Shop.ShopId == shopID && o.Tb_OrderBag_BK.OrderTime > startTime && o.Tb_OrderBag_BK.OrderTime < endTime).ToList();
                    rtnList.AddRange(OrderBKConvertToList(db, listOrderBK));
                }
                //if (!string.IsNullOrEmpty(ctmName))
                //{
                //    List<orderInfo> listOrder = db.orderInfo.Where(o => o.Tb_OrderBag.Shop.ShopId == shopID  && (o.CtmName.Contains(ctmName) || o.OrderProductName.Contains(ctmName))).ToList();
                //    rtnList.AddRange(OrderConvertToList(db, listOrder));
                //    List<orderInfo_BK> listOrderBK = db.orderInfo_BK.Where(o => o.Tb_OrderBag_BK.Shop.ShopId == shopID && (o.CtmName.Contains(ctmName) || o.OrderProductName.Contains(ctmName))).ToList();
                //    rtnList.AddRange(OrderBKConvertToList(db, listOrderBK));
                //}
                //else
                //{
                //    List<orderInfo> listOrder = db.orderInfo.Where(o => o.Tb_OrderBag.Shop.ShopId == shopID && o.Tb_OrderBag.OrderTime > startTime && o.Tb_OrderBag.OrderTime < endTime).ToList();
                //    rtnList.AddRange(OrderConvertToList(db, listOrder));
                //    List<orderInfo_BK> listOrderBK = db.orderInfo_BK.Where(o => o.Tb_OrderBag_BK.Shop.ShopId == shopID && o.Tb_OrderBag_BK.OrderTime > startTime && o.Tb_OrderBag_BK.OrderTime < endTime).ToList();
                //    rtnList.AddRange(OrderBKConvertToList(db, listOrderBK));
                //}
                return rtnList;

            }

        }
        List<OrderInfoQuary> OrderConvertToList(newSampleYPDBEntities db, List<orderInfo> listOrder)
        {
            List<OrderInfoQuary> listRtn = new List<OrderInfoQuary>();
            foreach (orderInfo order in listOrder)
            {
                OrderInfoQuary info = new OrderInfoQuary();
                info.OrderProductName = order.OrderProductName;
                info.OrderProductId = order.OrderProductId;
                info.CtmName = order.CtmName;
                info.Height = (float)order.Height;
                info.Width = (float)order.Width;
                info.productNum = order.productNum;
                order.Tb_OrderBagReference.Load();
                info.CreateTime = order.Tb_OrderBag.OrderTime;
                info.FatchTime = order.Tb_OrderBag.FetchTime;
                order.WorkOrderFlowList.Load();
                WorkOrderFlowList wofl = order.WorkOrderFlowList.FirstOrDefault(p => p.Status == 1);
                if (wofl != null)
                {
                    wofl.tb_TechReference.Load();
                    info.TechName = wofl.tb_Tech.TechName;
                }
                else
                {
                    int compInt = order.WorkOrderFlowList.Where(p => p.Status == 4).Count();
                    int allCount = order.WorkOrderFlowList.Count();
                    if (compInt == allCount)
                    {
                        info.TechName = "生产完成";
                    }
                    else
                    {
                        info.TechName = "暂停";
                    }
                   
                }
                info.PrePriceCount = (decimal)order.PrePriceCount;
                info.Area = order.Area;
                info.PhotoList = new List<PhotoInfoQuery>();
                order.photoInfo.Load();
                foreach (photoInfo photo in order.photoInfo)
                {
                    PhotoInfoQuery ph = new PhotoInfoQuery();
                    ph.photoName = photo.photoName;
                    ph.TotalNum = (int)photo.TotalNum;
                    ph.Width = (float)photo.Width;
                    ph.Height = (float)photo.Height;
                    info.PhotoList.Add(ph);
                }

                listRtn.Add(info);
            }
            return listRtn;
        }

        List<OrderInfoQuary> OrderBKConvertToList(newSampleYPDBEntities db, List<orderInfo_BK> listOrder)
        {
            List<OrderInfoQuary> listRtn = new List<OrderInfoQuary>();
            foreach (orderInfo_BK order in listOrder)
            {
                OrderInfoQuary info = new OrderInfoQuary();
                info.OrderProductName = order.OrderProductName;
                info.OrderProductId = order.OrderProductId;
                info.CtmName = order.CtmName;
                info.Height = (float)order.Height;
                info.Width = (float)order.Width;
                info.productNum = order.productNum;
                order.Tb_OrderBag_BKReference.Load();
                info.CreateTime = order.Tb_OrderBag_BK.OrderTime;
                info.FatchTime = order.CompleteTime;
                //order.WorkOrderFlowList.Load();
                
                    info.TechName = "完成";
               
                info.PrePriceCount = (decimal)order.PrePriceCount;
                info.Area = order.Area;
                info.PhotoList = new List<PhotoInfoQuery>();
                order.photoInfo_BK.Load();
                foreach (photoInfo_BK photo in order.photoInfo_BK)
                {
                    PhotoInfoQuery ph = new PhotoInfoQuery();
                    ph.photoName = photo.photoName;
                    ph.TotalNum = (int)photo.TotalNum;
                    ph.Width = (float)photo.Width;
                    ph.Height = (float)photo.Height;
                    info.PhotoList.Add(ph);
                }

                listRtn.Add(info);
            }
            return listRtn;
        }
        /// <summary>
        /// 根据条件组织账单信息
        /// </summary>
        /// <param name="inArray"></param>
        /// <returns></returns>
        public List<SHOP_Bill_Info> BillQuery(string[] inArray, int inShopID)
        {
            List<SHOP_Bill_Info> rtnList = new List<SHOP_Bill_Info>();
            string ctmName = inArray[0];
            int billID = string.IsNullOrEmpty(inArray[1]) ? 0 : Convert.ToInt32(inArray[1]);
            DateTime startTime = Convert.ToDateTime(inArray[2]);
            DateTime endTime = Convert.ToDateTime(inArray[3]);
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                if (string.IsNullOrEmpty(ctmName) && billID == 0)
                {
                    List<SHOP_Bill> shopBillList = db.SHOP_Bill.Where(sb => sb.Shop.ShopId==inShopID&& sb.CreateTime > startTime && sb.CreateTime < endTime).ToList();
                    rtnList = ConvertBillTOView(db, shopBillList);
                }
                else if (!string.IsNullOrEmpty(ctmName) && billID == 0)
                {
                    List<SHOP_Bill> shopBillList = db.SHOP_Bill.Where(sb => sb.Shop.ShopId == inShopID && sb.CreateTime > startTime && sb.CreateTime < endTime && sb.Bill_Detail.Where(bd => bd.CtmName.Contains(ctmName)).Count() > 0).ToList();
                    rtnList = ConvertBillTOView(db, shopBillList);
                }
                else if (string.IsNullOrEmpty(ctmName) && billID != 0)
                {
                    List<SHOP_Bill> shopBillList = db.SHOP_Bill.Where(sb => sb.Shop.ShopId == inShopID && sb.BillId == billID).ToList();
                    rtnList = ConvertBillTOView(db, shopBillList);
                }
                else if (!string.IsNullOrEmpty(ctmName) && billID != 0)
                {
                    List<SHOP_Bill> shopBillList = db.SHOP_Bill.Where(sb => sb.Shop.ShopId == inShopID && sb.BillId == billID).ToList();
                    rtnList = ConvertBillTOView(db, shopBillList);
                }


            }


            return rtnList;
        }

        List<SHOP_Bill_Info> ConvertBillTOView(newSampleYPDBEntities db, List<SHOP_Bill> inBill)
        {
            List<SHOP_Bill_Info> listShopBill = new List<SHOP_Bill_Info>();
            foreach (SHOP_Bill shopBill in inBill)
            {
                SHOP_Bill_Info sbi = new SHOP_Bill_Info();
                //shopBill.ShopReference.Load();
                shopBill.Bill_Detail.Load();
                

                sbi.List_Detail = new List<Bill_Detail_Info>();
                //shopBill.Bill_Detail.ToList().ForEach(
                //    p => { 
                //        sbi.List_Detail.Add(new Bill_Detail_Info(){BillDetailId=p.BillDetailId, CtmName=p.CtmName,

                //            ListDetail_Detail=new List<Bill_Detail_Detail_Info>(),

                //            OrderInfoCode=p.OrderInfoCode, OrderProductName=p.OrderProductName, Remark=p.OiRemark})
                //    }
                //    );
                foreach (Bill_Detail bd in shopBill.Bill_Detail)
                {
                    Bill_Detail_Info bdi = new Bill_Detail_Info();
                    bdi.BillDetailId = bd.BillDetailId;
                    bdi.CtmName = bd.CtmName;
                    bdi.OrderInfoCode = bd.OrderInfoCode;
                    bdi.OrderProductName = bd.OrderProductName;
                    bdi.Remark = bd.OiRemark;
                    bdi.ListDetail_Detail = new List<Bill_Detail_Detail_Info>();
                    bd.Bill_Detail_Detail.Load();
                    foreach (Bill_Detail_Detail bdd in bd.Bill_Detail_Detail)
                    {
                        Bill_Detail_Detail_Info bddi = new Bill_Detail_Detail_Info() {   Bill_Detail_DetailId=bdd.Bill_Detail_DetailId,Area = bdd.Area, Count = bdd.Count, DetailName = bdd.DetailName, Num = bdd.Num, Spec = bdd.Spec, Type = (int)bdd.Type, UnitPrice = bdd.UnitPrice };
                        bdi.ListDetail_Detail.Add(bddi);
                    }
                    sbi.List_Detail.Add(bdi);
                }
                sbi.BillCount = (double)shopBill.TotalPrice;
                sbi.ShopBalance = (double)shopBill.ShopBalance;
                sbi.CreateTime = shopBill.CreateTime;
                sbi.BillOrderCode = shopBill.BillOrderCode;
                sbi.BillId = shopBill.BillId;
                listShopBill.Add(sbi);
            }
            return listShopBill;

        }
        /// <summary>
        /// 获取商户的当前余额
        /// </summary>
        /// <param name="inShopId"></param>
        /// <returns></returns>
        public double GetShopBlance(int inShopId)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Shop shop = db.Shop.FirstOrDefault(p => p.ShopId == inShopId);
                return shop.Balance == null ? 0 : (double)shop.Balance;
            }
        }
    }
}
